var Report = Report || {};

Report = {
	poiId:'910A',
	detailContent:'',
	init: function(){
		var self = this;
		//self.getReport('reportList');
		// 禁言弹窗关闭 
		$('.gag-tips').click(function(){
			$(this).fadeOut();
			$('.gag-list').css("bottom", "-5.44rem");
		})
		//禁言
		$('.gag-btn').click(function(e){
			e.stopPropagation();
			var _self = $(this),
				txt = _self.html();
			AlertBox({type:'doubleBtn',msg:'是否确定'+txt+'？',alertCls:'alert-double',confirm:function(){
					var reportId = _self.parent().attr('data-id'),
						type = _self.parent().attr('data-type'),
						day = _self.attr('data-day');
					self.reportHandle(reportId, type, '3', day);
					// 关闭弹窗
					$('.gag-tips').fadeOut();
					$('.gag-list').css("bottom", "-5.44rem");
				}})
		})
		// 举报处理
		$('.report-content')
			//弹窗 
			.on('click','.btn-delProhi',function(){
				var reportId = $(this).parent().attr('data-id');
				var reportType = $(this).parent().attr('data-type');
				$('.gag-tips').fadeIn();
				$('.gag-list')
					.css("bottom", "0")
					.attr({'data-id': reportId, 'data-type': reportType});

			})
			// 忽略
			.on('click','.btn-ignore',function(){
				var _self = $(this);
				AlertBox({type:'doubleBtn',msg:'是否确定忽略本条举报？',alertCls:'alert-double',confirm:function(){
					var reportId = _self.parent().attr('data-id'),
						type = _self.parent().attr('data-type');
					self.reportHandle(reportId, type, '1');
				}})
			})
			// 删除帖子
			.on('click','.btn-del',function(){
				var _self = $(this);
				AlertBox({type:'doubleBtn',msg:'是否确定删除本条帖子？',alertCls:'alert-double',confirm:function(){
					var reportId = _self.parent().attr('data-id'),
						type = _self.parent().attr('data-type');
					self.reportHandle(reportId, type, '2');
				}})
			})
			// 解除禁言
			.on('click','.btn-relieve',function(){
				var _self = $(this);
				AlertBox({type:'doubleBtn',msg:'是否确定解除该用户的禁言？',alertCls:'alert-double',confirm:function(){
					var reportId = _self.parent().attr('data-id'),
						type = _self.parent().attr('data-type');
					self.reportHandle(reportId, type, '4');
				}})
			})
			// 恢复
			.on('click','.btn-recovery',function(){
				var _self = $(this);
				AlertBox({type:'doubleBtn',msg:'是否确定恢复本条帖子？',alertCls:'alert-double',confirm:function(){
					var reportId = _self.parent().attr('data-id'),
						type = _self.parent().attr('data-type');
					self.reportHandle(reportId, type, '5');
				}})
			})
			// 跳转到举报详情
			.on('click','#reportList .act-con', function(){
				var _self = $(this);
				var reportId = _self.attr('data-id');
				var reportType = _self.attr('data-type');
				self.detailContent = _self;
				Q.go('datail/report/'+reportId+'/'+reportType);
			})
			// 跳转到处理详情
			.on('click','#dealList .act-con', function(){
				var _self = $(this);
				var reportId = _self.attr('data-id');
				var reportType = _self.attr('data-type');
				self.detailContent = _self;
				Q.go('datail/deal/'+reportId+'/'+reportType);
			})
		// 处理类型
		template.helper('status', function(a,b){
			var arr = ['忽略','删除','删除并禁言','解除禁言','恢复'];
			if (a==2) {
				return '<span class="red">' + arr[a-1] + '</span>';
			}else if(a==3){
				if (b==-1) {
					return '<span class="red">删除并永久禁言</span>';
				}else{
					return '<span class="red">' + arr[a-1]+b+'天</span>';
				}
			}else if(a==4||a==5){
				return '<span class="blue">' + arr[a-1] + '</span>';
			}else{
				return '<span>' + arr[a-1] + '</span>';
			};
		})
		// 举报理由
		template.helper('reType', function(a){
			var arr = ['低俗骚扰','暴力色情','人身攻击','广告','其他'];
			return arr[a];
		})
		// time
		template.helper('time', function(a){
			if (!a) {return};
			var stamp = a;
			return self.getDateDiff(stamp)
		})
		// time
		template.helper('date', function(a){
			if (!a) {return};
			var now = new Date(a);
		    var year=now.getFullYear(); 
			var month=now.getMonth()+1; 
			var date=now.getDate(); 
			var hour=now.getHours(); 
			var minute=now.getMinutes(); 
			var second=now.getSeconds(); 
			return year+"年"+month+"月"+date+"日 "+hour+":"+minute+":"+second; 
		})
		template.config("escape", false);
	},
	// 获取列表 obj：列表id（容器id,模板id,接口地址保持一致）
	getReport: function(obj,page){
		var self = this,
			obj = obj,
			loading = $('<p class="loading"><i></i>正在加载...</p>'),
			nodata = $('<p class="loading">没有更多了~</p>'),
			neterr = $('<p class="loading">网络异常</p>'),
			actList = $('#'+obj).find('.act-area').length;
		var pageNum = page?++page:1;
		pageNum==1&&$('#'+obj).html("");
		$('.p-msg').html(loading);
		$.ajax({
			url: baseurl + '/post/query/'+obj+'.do',
			type: 'GET',
			data: {
				pageNum: pageNum
			},
			dataType: 'jsonp',
			success: function(data){
				//loading.remove();
				if (!data||!data.result) {
					$('.p-msg').html(nodata);
					return;
				};
				if (data.resultCode!=0) {
					AlertBox({type:'mini',msg:data.resultMsg,alertCls:'alert-mini'});
					return;
				};
				var html = template(obj+'Model', data);
				$('#'+obj).append(html);
				lazyelem.listen('img[lazy-src]', 'img', function(obj) {
					obj.each(function(){
	                    this.onerror = function() {
	                        this.src = '/duimen/css/images/empty.png';
	                    }
                	})
                });
				if (data.result.length<10) {
					$('.p-msg').html(nodata);
					return;
				};
				lazyelem.listen('.p-msg', 'fn', function() {
                    self.getReport(obj, pageNum);
                });
			},
			error: function(){
				console.log('请求错误')
				$('#'+obj).html(neterr);
			}
		})
	},
	// 获取详情 obj：接口id
	getDetail: function(obj,id,type){
		var self = this,
			obj = obj,
			loading = $('<p class="loading"><i></i>正在加载...</p>'),
			nodata = $('<p class="loading">没有更多了~</p>'),
			neterr = $('<p class="loading">网络异常</p>'),
			actList = $('#'+obj).find('.act-area').length;
		//if (actList) {return};
		$('.p-msg').html(loading);
		$.ajax({
			url: baseurl + '/post/query/'+obj+'.do',
			type: 'GET',
			data: {
				relationId: id,
				relationType: type
			},
			dataType: 'jsonp',
			success: function(data){
				loading.remove();
				if (!data||!data.result) {
					$('#'+obj).html(nodata);
					return;
				};
				if (data.resultCode!=0) {
					AlertBox({type:'mini',msg:data.resultMsg,alertCls:'alert-mini'});
					return;
				};
				var html = template(obj+'Model', data);
				$('#'+obj).html(html);
				lazyelem.listen('img[lazy-src]', 'img', function(obj) {
					obj.each(function(){
	                    this.onerror = function() {
	                        this.src = '/duimen/css/images/empty.png';
	                    }
                	})
                });
				// 获取更多历史
				if ((data.result.reportList&&data.result.reportList.length>=10)||(data.result.dealList&&data.result.dealList.length>=10)) {
					self.getDetailMore(obj+'More', id, type);
				}else{
					$('.p-msg').html(nodata);
				};
			},
			error: function(){
				console.log('请求错误')
				$('#'+obj).html(neterr);
			}
		})
	},
	// 获取详情更多 obj：接口id
	getDetailMore: function(obj,id,type,page){
		var self = this,
			obj = obj,
			loading = $('<p class="loading"><i></i>正在加载...</p>'),
			nodata = $('<p class="loading">没有更多了~</p>'),
			neterr = $('<p class="loading">网络异常</p>'),
			actList = $('#'+obj).find('.act-area').length;
		var pageNum = page?++page:1;
		//pageNum==1&&$('#'+obj).html("");
		$('.p-msg').html(loading);
		$.ajax({
			url: baseurl + '/post/query/'+obj+'.do',
			type: 'GET',
			data: {
				relationId: id,
				relationType: type,
				pageNum: pageNum
			},
			dataType: 'jsonp',
			success: function(data){
				//loading.remove();
				if (!data||!data.result) {
					$('.p-msg').html(nodata);
					return;
				};
				if (data.resultCode!=0) {
					AlertBox({type:'mini',msg:data.resultMsg,alertCls:'alert-mini'});
					return;
				};
				var html = template(obj+'Model', data);
				$('#'+obj).append(html);
				if (data.result.length<10) {
					$('.p-msg').html(nodata);
					return;
				};
				lazyelem.listen('.p-msg', 'fn', function() {
                    self.getDetailMore(obj, id, type, pageNum);
                });
			},
			error: function(){
				console.log('请求错误')
				$('.p-msg').html(neterr);
			}
		})
	},
	// 举报处理
	reportHandle: function(id,relationType, dealType, days){
		var days = days || "0";
		$.ajax({
			url: baseurl + '/post/reportDeal.do',
			type: 'GET',
			data: {
				relationId: id,
				relationType: relationType,
				dealType: dealType,
				forbiddenDays: days
			},
			dataType: 'jsonp',
			jsonpCallback: 'getList',
			success: function(data){
				if (!data) { AlertBox({type:'mini',msg:'操作失败，请稍后再试！',alertCls:'alert-mini'});return; };
				if (data.resultCode!=0) {
					AlertBox({type:'mini',msg:data.resultMsg,alertCls:'alert-mini'});
					return;
				};
				AlertBox({type:'mini',msg:'操作成功！',alertCls:'alert-mini'});
				location.reload();
			},
			error: function(){
				console.log('请求错误');
			}
		})
	},
	// 时间转换
	getDateDiff: function(dateTimeStamp){
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var now = new Date().getTime();
		var diffValue = now - dateTimeStamp;
		if(diffValue < 0){return;}
		var monthC =diffValue/month;
		var weekC =diffValue/(7*day);
		var dayC =diffValue/day;
		var hourC =diffValue/hour;
		var minC =diffValue/minute;
		if(monthC>=1){
			result="" + parseInt(monthC) + "月前";
		}
		else if(weekC>=1){
			result="" + parseInt(weekC) + "周前";
		}
		else if(dayC>=1){
			result=""+ parseInt(dayC) +"天前";
		}
		else if(hourC>=1){
			result=""+ parseInt(hourC) +"小时前";
		}
		else if(minC>=1){
			result=""+ parseInt(minC) +"分钟前";
		}else
		result="刚刚";
		return result;
	}
}

$(function(){
	Util.checkLogin(function(){
		Report.init();
	})
	// 举报,处理列表
	/*
	*举报 #!list/report
	*处理 #!list/deal
	*/
	Q.reg('list',function(type){
		$('#teb1 a').html('被举报列表').attr('href','#!list/report');
		$('#teb2 a').html('已处理列表').attr('href','#!list/deal');
		if (type == 'deal') {
			// 处理列表
			$('#teb2').addClass('curr').siblings().removeClass('curr');
			$('#dealList').show().siblings().hide();
			Report.getReport('dealList');
		}else{
			// 举报列表
			$('#teb1').addClass('curr').siblings().removeClass('curr');
			$('#reportList').show().siblings().hide();
			Report.getReport('reportList');
		};
	})
	/*
	*举报 #!datail/report/id
	*处理 #!datail/deal/id
	*/
	// 举报处理详情
	Q.reg('datail',function(type, id, reportType){
		$('#teb1 a').html('举报详情').attr('href','#!datail/report/'+id+'/'+reportType);
		$('#teb2 a').html('处理详情').attr('href','#!datail/deal/'+id+'/'+reportType);
		if (type == 'deal') {
			// 处理详情
			$('#teb2').addClass('curr').siblings().removeClass('curr');
			$('#dealHistory').show().siblings().hide();
			Report.getDetail('dealHistory', id, reportType); 
		}else{
			// 举报详情
			$('#teb1').addClass('curr').siblings().removeClass('curr');
			$('#reportHistory').show().siblings().hide();
			Report.getDetail('reportHistory', id, reportType);
		};
	})

	Q.init({
	    index:'list', /* 首页地址 */
	    pop:navchange=function(l){}
	});
})